package com.sun.lotter.luck;

import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.sun.base.BasePlayType;
import com.sun.base.UserActivity;
import com.sun.lotter.R;
import com.sun.net.LinkServer;
import com.sun.net.BaseDao.HttpListener;

public class LuckLotterActivity extends UserActivity implements OnClickListener {
	private TextView		lotterNameView	= null;
	private String[]		luckNameStrings	= { "星座选号器", "生肖选号器", "生日选号器", "姓名选号器", "摇摇乐" };
	private FrameLayout		contentLayout	= null;
	private int				luckIndex		= 0;
	private BasePlayType[]	playTypes		= new BasePlayType[luckNameStrings.length];
	private TextView		textView_issue;
	private ProgressBar		progressBar;
	
	@Override
	public void init() {
		noTitle = true;
		LayoutInflater.from(this).inflate(R.layout.luckmachine_1, mainLayout);
		contentLayout = (FrameLayout) mainLayout.findViewById(R.id.FrameLayout_Lucklayout);
		lotterNameView = (TextView) mainLayout.findViewById(R.id.TextView_Title);
		textView_issue = (TextView) mainLayout.findViewById(R.id.textview_issue);
		progressBar = (ProgressBar) mainLayout.findViewById(R.id.progressBar);
		lotterNameView.setText(luckNameStrings[0]);
		
		mainLayout.findViewById(R.id.Button_Left).setOnClickListener(this);
		mainLayout.findViewById(R.id.Button_Right).setOnClickListener(this);
		playTypes[0] = new LuckPlayPage1(this);
		playTypes[1] = new LuckPlayPage2(this);
		playTypes[2] = new LuckPlayPage3(this);
		playTypes[3] = new LuckPlayPage4(this);
		playTypes[4] = new LuckPlayPage5(this);
		contentLayout.addView(playTypes[0]);
		
		LinkServer ls = new LinkServer(handler);
		ls.GetIssue(lotterId, 1, new HttpListener() {
			
			@Override
			public void onEvent(final List<String> list) {
				progressBar.setVisibility(View.GONE);
				if (list.get(0).equals("000")) {
					textView_issue.setText(list.get(2));
				} else {
					textView_issue.setText("当前期号获取失败");
				}
				
			}
		});
	}
	
	@Override
	public void onPause() {
		playTypes[luckIndex].onEvent("off");
		super.onPause();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		playTypes[luckIndex].onEvent("on");
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.Button_Left:
				playTypes[luckIndex].onEvent("off");
				luckIndex--;
				if (luckIndex < 0) {
					luckIndex = luckNameStrings.length - 1;
				}
				playTypes[luckIndex].onEvent("on");
				lotterNameView.setText(luckNameStrings[luckIndex]);
				contentLayout.removeAllViews();
				contentLayout.addView(playTypes[luckIndex]);
			break;
			case R.id.Button_Right:
				playTypes[luckIndex].onEvent("off");
				luckIndex++;
				if (luckIndex >= luckNameStrings.length) {
					luckIndex = 0;
				}
				lotterNameView.setText(luckNameStrings[luckIndex]);
				contentLayout.removeAllViews();
				contentLayout.addView(playTypes[luckIndex]);
				playTypes[luckIndex].onEvent("on");
			break;
			
			default:
			break;
		}
	}
	
}
